<?php

function toArray($obj){
    $return=array();
    if (is_array($obj)) {
        $keys=array_keys($obj);
    } elseif (is_object($obj)) {
        $keys=array_keys(get_object_vars($obj));
    } else {return $obj;}
    foreach ($keys as $key) {
        $value=$obj->$key;
        if (is_array($obj)) {
            $return[$key]=toArray($obj[$key]);
        } else {
            $return[$key]=toArray($obj->$key);
        }
    }
    return $return;
} 

function toXML($obj, $name){
	$arr = toArray($obj);
	$return = "<?xml version='1.0' encoding='ISO-8859-1'?>";
	$return .= "<?xml-stylesheet type='text/xsl' href='simple.xsl' ?>";
	
	$return .= "<".$name.">";
	foreach($arr as $a){	
		$return .= loopArray($a, "general_ledger");
	}
	$return .= "</".$name.">";
	
	return $return;
}

function isNumericChild($arr){

	$answer = true;

	if(is_array($arr)){
		$keys = array_keys($arr);
		foreach($keys as $key){
			if(!is_numeric($key)){
				$answer = false;
			}
		}
	}else{
		return false;
	}
	
	return $answer;
	
}

function loopArray($arr, $name){
	$return = "";
	$keys = array_keys($arr);
	foreach($keys as $key){
		if(is_numeric($key)){
			$return .= "<".$name.">";
		}elseif(!isNumericChild($arr[$key])){
			$return .= "<".$key.">";
		}
		
		if(is_array($arr[$key])){
			$return .= loopArray($arr[$key], $key);
		}else{
			$return .= $arr[$key];
		}
		
		if(is_numeric($key)){
			$return .= "</".$name.">";
		}elseif(!isNumericChild($arr[$key])){
			$return .= "</".$key.">";
		}
	}
	return $return;
}

?>